using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;

namespace M4W.HorizontalTabs
{
    [Guid("603b8050-fcc7-4914-901c-42ff4d8d448b")]
    public class M4WHorizontalTabsEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            // Delete Web Part

            SPSite site = properties.Feature.Parent as SPSite;
            if (site != null)
            {
                try
                {
                    SPList wpGallery = site.GetCatalog(SPListTemplateType.WebPartCatalog);
                    SPQuery query = new SPQuery();
                    query.Query = "<Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='Text'>M4W.HorizontalTabs.webpart</Value></Eq></Where>";
                    SPListItemCollection items = wpGallery.GetItems(query);
                    if (items.Count > 0)
                    {
                        items[0].Delete();
                    }
                }
                catch (Exception)
                {
                }
            }
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }
    }
}
